package com.lanou3g.musicenjoy.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.SingerContentAdapter;
import com.lanou3g.musicenjoy.entity.PlayMusicEntity;
import com.lanou3g.musicenjoy.entity.SingerContentEntity;
import com.lanou3g.musicenjoy.eventbus.MyEvent;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 15/10/23.
 */
public class SingerContentFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ImageView singerImg;
    private ListView songerList;
    private String url;
    private RequestQueue requestQueue;
    private List<SingerContentEntity.DataEntity> data;
    private SingerContentAdapter adapter;
    private List<PlayMusicEntity> datas=new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.singer_list, null);
        singerImg = (ImageView) view.findViewById(R.id.singer_img);
        songerList = (ListView) view.findViewById(R.id.songer_list);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        requestQueue = VolleySingleton.getInstance(getActivity()).getRequestQueue();
        singerImg.setOnClickListener(this);
        songerList.setOnItemClickListener(this);
        adapter = new SingerContentAdapter(getActivity());
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        url = "http://api.dongting.com/song/singer/" + id + "/songs?uid=862136025639005&f=3030&os=4.2.2&app=ttpod&hid=6143071666190381&imei=862136025639005&alf=702006&from=android&resolution=720x1280&net=2&api_version=1.0&agent=none&size=30&v=v8.2.0.2015091720&utdid=U157OqK3fvMBAPX2Buw9EsPo&s=s200&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314&page=1&longitude=0.0&user_id=0&latitude=0.0&language=zh&imsi=460078098497828&tid=0";
        Log.i("wangwanglll", url);
        getInternetData();
    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SingerContentEntity entity = gson.fromJson(s, SingerContentEntity.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
                    songerList.setAdapter(adapter);
                    adapter.setDatas(data);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (data.get(position).getAuditionList().size() > 0) {
            for (SingerContentEntity.DataEntity dataEntity : data) {
                PlayMusicEntity musicEntity = new PlayMusicEntity();

                    musicEntity.setSongName(dataEntity.getName());
                    musicEntity.setSingerName(dataEntity.getSingerName());

                if (dataEntity.getMvList().size()>0) {
                    musicEntity.setPicUrl(dataEntity.getMvList().get(0).getPicUrl());
                }
                if (dataEntity.getUrlList().size()>0) {
                    musicEntity.setUrl(dataEntity.getUrlList().get(0).getUrl());
                }
                    datas.add(musicEntity);
                    Log.i("sb",datas+"");



            }
            EventBus.getDefault().post(new MyEvent.PassData(datas, position));
        } else {

            Toast.makeText(getActivity(), "由于版权问题,当前音乐无法播放", Toast.LENGTH_SHORT).show();
        }
    }
}